package zhibi.wechat.mp.controller;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zhibi.commons.response.JsonResult;
import zhibi.wechat.mp.extra.base.BaseController;

import java.net.URLEncoder;
import java.util.*;

import static zhibi.wechat.mp.extra.context.BaseConfig.REQUEST_PREFIX;

@Controller
@RequestMapping(REQUEST_PREFIX + "menu")
public class WxMpMenuController extends BaseController {

    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxMpConfigStorage wxMpConfigStorage;

    @RequestMapping("index")
    public String index() {
        return VIEW_PREFIX + "menu/index";
    }

    /**
     * 现在的菜单
     *
     * @return
     * @throws WxErrorException
     */
    @RequestMapping("menuList")
    @ResponseBody
    public List<WxMenuButton> menuList() throws WxErrorException {
        //所有的菜单
        WxMpMenu wxMpMenu = wxMpService.getMenuService().menuGet();
        return wxMpMenu.getMenu().getButtons();
    }

    /**
     * 更新菜单
     *
     * @return
     */
    @RequestMapping("updateMenu")
    @ResponseBody
    public JsonResult<String> updateMenu(@RequestBody List<WxMenuButton> menuButtons) throws WxErrorException {
        for (WxMenuButton button : menuButtons) {
            if (button.getUrl() != null && !Objects.equals(button.getUrl(), "")) {
                button.setUrl(OAUTH_URL.replace("APPID", wxMpConfigStorage.getAppId())
                        .replace("REDIRECT_URI", URLEncoder.encode(button.getUrl())));
            }
            List<WxMenuButton> buttons = button.getSubButtons();
            if (buttons != null) {
                for (WxMenuButton item : buttons) {
                    if (item.getUrl() != null && !Objects.equals(item.getUrl(), "")) {
                        item.setUrl(OAUTH_URL.replace("APPID", wxMpConfigStorage.getAppId())
                                .replace("REDIRECT_URI", URLEncoder.encode(item.getUrl())));
                    }
                }
            }
        }
        WxMenu wxMenu = new WxMenu();
        wxMenu.setButtons(menuButtons);
        String s = wxMpService.getMenuService().menuCreate(wxMenu);
        System.out.println("更新菜单：" + s);
        return JsonResult.success("success");
    }

    /**
     * 分页获取图文消息列表
     *
     * @param pageNum
     * @return
     * @throws WxErrorException
     */
    @RequestMapping("newsListByPage")
    @ResponseBody
    public JsonResult<List<Map<String, String>>> newsListByPage(int pageNum) throws WxErrorException {
        List<Map<String, String>> list = new ArrayList<>();
        //所有的图文消息
        WxMpMaterialNewsBatchGetResult result = wxMpService.getMaterialService().materialNewsBatchGet((pageNum - 1) * pageSize, pageSize);
        if (null != result && result.getItems() != null) {
            for (WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem item : result.getItems()) {
                Map<String, String> map = new HashMap<>();
                map.put("MediaId", item.getMediaId());
                map.put("Name", item.getContent().getArticles().get(0).getTitle());
                list.add(map);
            }
        }
        return JsonResult.success(list);
    }

    public static void main(String[] args) {
        System.out.println(OAUTH_URL.replace("APPID", "wxef662811af488336")
                .replace("REDIRECT_URI", URLEncoder.encode("http://www.jiangwp.cn/wxweb/logoin.html")));
    }
}
